package com.atm.servlet;

import com.atm.pojo.RunMessage;
import com.atm.pojo.User;
import com.atm.service.RunMessageService;
import com.atm.service.UserService;
import com.atm.service.impl.RunMessageServiceImpl;
import com.atm.service.impl.UserServiceImpl;
import com.atm.utill.MailUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * 用户转账
 */
@WebServlet("/UserTransfer")
public class UserTransferServlet extends HttpServlet {
    private UserService userService=new UserServiceImpl();
    private RunMessageService runMessageService=new RunMessageServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String transferIn = req.getParameter("transferIn");
        String transferNum = req.getParameter("transferNum");
        String icno = null;
        if (req.getSession().getAttribute("ua")!=null){
            User user=(User)req.getSession().getAttribute("ua");
            icno = user.getIcno();
        }
        int money=0;
        if (transferNum != null){
            money=Integer.valueOf(transferNum);
        }
        if (userService.getUserByIcno(transferIn)==null){
            req.getSession().setAttribute("error4","用户不存在");
            resp.sendRedirect("/changepage/UserTransfer.jsp");
        }
        double userBalance = userService.getUserBalance(icno);
        if (money==0){
            req.getSession().setAttribute("error4","转账不能为0");
            resp.sendRedirect("/changepage/UserTransfer.jsp");
        }else if (money>2000){
            req.getSession().setAttribute("error4","转账不能大于2000");
            resp.sendRedirect("/changepage/UserTransfer.jsp");
        }else if (money%50!=0){
            req.getSession().setAttribute("error4","转账为50的倍数");
            resp.sendRedirect("/changepage/UserTransfer.jsp");
        }else if (userBalance<money){
            req.getSession().setAttribute("error4","转账不能超过自己的金额");
            resp.sendRedirect("/changepage/UserTransfer.jsp");
        }else {
            //转账操作
            boolean result=userService.transfer(transferIn,icno,money);
            //记录
            //保存交易记录
            RunMessage runMessage = new RunMessage();
            runMessage.setIcno(transferIn);
            runMessage.setRm_date(new Date().toString());
            runMessage.setRm_balance(Double.parseDouble(String.valueOf(money)));
            runMessage.setRm_rest(userService.getUserBalance(transferIn));
            runMessage.setRm_currency("RMB");
            if(result){
                runMessageService.addRunMessage(runMessage);
                runMessage.setIcno(icno);
                runMessage.setRm_rest(userService.getUserBalance(icno));
                runMessage.setRm_balance(-Double.parseDouble(String.valueOf(money)));
                runMessageService.addRunMessage(runMessage);
                String email = userService.getEmailByIcno(icno);
                String content="您的卡号为:"+icno+" 的账户向"+transferIn+"账户转账"+money+"\n 剩余金额为:"+userService.getUserBalance(icno);
                MailUtils.sendEmail(email,"ATM模拟系统转账",content);
                resp.sendRedirect("/UserSerch");
            }else {
                req.getSession().setAttribute("error4","转账失败");
                resp.sendRedirect("/changepage/UserTransfer.jsp");
            }
        }
    }
}
